package com.xyht.sca_s.student_manage_system.modules.asset.util;

import com.xyht.sca_s.student_manage_system.modules.asset.entity.CategoryNode;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class CategoryUtil {

    public String generateCategoryID(String root_parent_id,Integer child_num){
        int child_num_new = (child_num+1);
        String child_num_str = Integer.toString(child_num_new);
        StringBuilder StringBuilder = new StringBuilder();
        StringBuilder.append(root_parent_id);
        for (int i = 0;i<4-child_num_str.length();i++){
            StringBuilder.append("0");
        }
        StringBuilder.append(child_num_str);
        return StringBuilder.toString();
    }


    public CategoryNode getChildrenNode(CategoryNode menu, List<CategoryNode> categoryList) {
        List<CategoryNode> children = categoryList.stream()
                .filter(subCategory -> subCategory.getParentId()
                        .equals(menu.getId()))
                .map(subCategory -> getChildrenNode(subCategory, categoryList))
                .collect(Collectors.toList());
        menu.setChildren(children);
        return menu;
    }

    public List<String> getChildrenIDList(CategoryNode categoryNode,List<String> childrenIDList){
        if (categoryNode.getChildren() == null || categoryNode.getChildren().size() < 1){
            return childrenIDList;
        }
        for (CategoryNode node:categoryNode.getChildren()) {
            childrenIDList.add(node.getId());
            getChildrenIDList(node,childrenIDList);
        }
        return childrenIDList;
    }


}
